local extension = Package:new("heg_diy")
local H = require "packages/hegemony/util"
local U = require "packages/utility/utility"
extension.extensionName = "love"

local heg = Fk.game_modes["new_heg_mode"]
table.insertTable(heg.whitelist, {"heg_diy"})




local qinlang = General(extension, "diy_heg__qinlang", "wei", 4)

local haochong = fk.CreateTriggerSkill{
    name = "diy_heg__haochong",
    events = {fk.TurnEnd},
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) then
            local events = player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
                for _, move in ipairs(e.data) do
                    if move.to == player.id and move.toArea == Card.PlayerHand then
                        return true
                    end
                end
            end, Player.HistoryTurn)
            return #events == 1
        end
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local use = room:askForUseCard(player, "slash", "slash", "#love_haochong-use", true, {bypass_times = true})
        if use  then
            use.extraUse = true
            self.cost_data = use
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:useCard(self.cost_data)
    end
}

qinlang:addSkill(haochong)

Fk:loadTranslationTable{
    ["diy_heg__qinlang"] ="秦朗",
    ["diy_heg__haochong"] ="昊宠",
    [":diy_heg__haochong"]="一名角色的回合结束时，若你于本回合获得过牌，你可以使用一张【杀】。",
}

local chengyu = General(extension, "diy_heg__chengyu", "wei", 3)

local shefu = fk.CreateTriggerSkill{
    name = "diy_heg__shefu",
    events = {fk.GeneralRevealed},
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) and H.compareKingdomWith(target, player) and not target.dead and target.phase == Player.NotActive then
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room =player.room
        local  use = U.askForUseVirtualCard(room,target,"slash",nil,self.name,"#diy_heg__shefu_use:"..room.current.id,true, true,true, true, {must_targets = {room.current.id}},true)
        if use then
            self.cost_data = use
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
       room:useCard(self.data)
    end
}
local benyu = fk.CreateTriggerSkill{
    name = "diy_heg__benyu",
    events = {fk.Damaged},
    can_trigger = function(self, event, target, player, data)
        return target == player and player:hasSkill(self) and data.from and not data.from.dead
                and (player:getHandcardNum() < math.min(data.from:getHandcardNum(), 5) or #player:getCardIds("he") > data.from:getHandcardNum())
    end,
    on_cost = function(self, event, target, player, data)
        local room = player.room
        local success, dat = room:askForUseActiveSkill(player, "ty__benyu_active", nil, true,
                {ty__benyu_data = {data.from.id, data.from:getHandcardNum()}})
        if success and dat then
            room:doIndicate(player.id, {data.from.id})
            self.cost_data = dat
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        if self.cost_data.interaction == "ty__benyu_damage" then
            player.room:throwCard(self.cost_data.cards, self.name, player, player)
            player.room:damage{
                from = player,
                to = data.from,
                damage = 1,
                skillName = self.name,
            }
        else
            player:drawCards(math.min(5, data.from:getHandcardNum()) - player:getHandcardNum())
        end
    end,
}

local benyu_active = fk.CreateActiveSkill{
    name = "diy_heg__benyu_active",
    prompt = function (self)
        local to = self.ty__benyu_data[1]
        local x = self.ty__benyu_data[2]
        if self.interaction.data == "ty__benyu_damage" then
            return "#ty__benyu-discard::"..to..":"..(x+1)
        else
            return "#ty__benyu-draw:::"..math.min(5, x)
        end
    end,
    interaction = function(self)
        local all_choices = {"ty__benyu_draw", "ty__benyu_damage"}
        local choices = {}
        if Self:getHandcardNum() < math.min(self.ty__benyu_data[2], 5) then
            table.insert(choices, all_choices[1])
        end
        if #Self:getCardIds("he") > self.ty__benyu_data[2] then
            table.insert(choices, all_choices[2])
        end
        if #choices > 0 then
            return UI.ComboBox { choices = choices, all_choices = all_choices }
        end
    end,
    target_num = 0,
    card_filter = function(self, to_select, selected)
        return self.interaction.data == "ty__benyu_damage" and not Self:prohibitDiscard(Fk:getCardById(to_select))
    end,
    feasible = function(self, selected, selected_cards)
        if self.interaction.data == "ty__benyu_damage" then
            return #selected_cards > self.ty__benyu_data[2]
        end
        return #selected_cards == 0
    end,
}
Fk:addSkill(benyu_active)
chengyu:addSkill(shefu)
chengyu:addSkill(benyu)

Fk:loadTranslationTable{
    ["diy_heg__chengyu"] ="程昱",
    ["diy_heg__shefu"] ="设伏",
    [":diy_heg__shefu"]="与你势力相同的角色于其回合外明置武将牌后，其可以对当前回合角色使用一张【杀】",
    ["#diy_heg__shefu_use"] = "设伏:视为对 %src 使用一张【杀】",

    ["diy_heg__benyu"] = "贲育",
    [":diy_heg__benyu"] = "当你受到伤害后，你可以选择一项：1.将手牌摸至X张（至多摸至五）；2.弃置至少X+1张牌，对伤害来源造成1点伤害（X为伤害来源的手牌数）。",
    ["ty__benyu_active"] = "贲育",
    ["#ty__benyu-discard"] = "贲育：你可以弃置至少%arg牌，对 %dest 造成1点伤害",
    ["#ty__benyu-draw"] = "贲育：你可以摸至 %arg 张牌",
    ["ty__benyu_draw"] = "摸牌",
    ["ty__benyu_damage"] = "弃牌并造成伤害",
    ["#ty__shefu_ambush"] = "设伏",
}
return extension
